package lava.rt.wrapper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class GetSetFieldWarpper extends SelfWrapper<Field> {

	public final Method getter,setter;
	
	public GetSetFieldWarpper(Field self) throws NoSuchMethodException, SecurityException {
		super(self);
		getter=initGetter(self);
		setter=initSetter(self);
	}

	private static Method initGetter(Field field) throws NoSuchMethodException, SecurityException {
		Method ret=null;
		String getterName="get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
		try {
			ret=field.getDeclaringClass().getMethod(getterName);
		} catch (NoSuchMethodException | SecurityException e) {
		}
		if(ret!=null)return ret;
		
		String[] names=field.getName().split("_");
		for(int i=0;i<names.length;i++) {
			if(names[i].trim().length()==0)continue;
			names[i]=names[i].substring(0,1).toUpperCase()+names[i].substring(1).toLowerCase();
		}
		getterName="get"+String.join("", names);
		ret=field.getDeclaringClass().getMethod(getterName);
		return ret;
	}

	private static Method initSetter(Field field) throws NoSuchMethodException, SecurityException {
		Method ret=null;
		String setterName="set"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
		try {
			ret=field.getDeclaringClass().getMethod(setterName,field.getType());
		} catch (NoSuchMethodException | SecurityException e) {
		}
		if(ret!=null)return ret;
		
		String[] names=field.getName().split("_");
		for(int i=0;i<names.length;i++) {
			if(names[i].trim().length()==0)continue;
			names[i]=names[i].substring(0,1).toUpperCase()+names[i].substring(1).toLowerCase();
		}
		setterName="set"+String.join("", names);
		ret=field.getDeclaringClass().getMethod(setterName,field.getType());
		return ret;
	}

	
	
}
